// composables/getData.ts
import type { LocationQuery } from "vue-router";
import type { NitroFetchRequest } from "nitropack";
import type { FetchOptions } from "ofetch";
// import fs from 'fs';

interface Params {
  url: NitroFetchRequest;
  opts: FetchOptions<any>;
  method?: "get" | "post" | "put";
}

export async function getFetchData(
  url: string,
  method = "GET",
  opts: object = {}
) {
  // 接口传参要求
  interface QueryItem {
    uid?: string;
    token?: LocationQuery;
  }
  const route = useRoute();
  const router = useRouter();
  const query: QueryItem = route.query;

  const config = useRuntimeConfig();

  let isTokenError = false;

  const res = await useFetch(url, {
    // method此处仅仅只处理了get与post请求
    method,
    // ofetch库会自动识别请求地址，对于url已包含域名的请求不会再拼接baseURL
    baseURL: "http://47.104.246.166:8088",
    // onRequest相当于请求拦截
    onRequest({ request, options }) {
      const user = useUseInfo();
      if (user.value) {
        const token = user.value.tokenString;
        options.headers = { ...options.headers, authorization: token };
      }
      // 设置请求参数
      if (method === "post" || method === "put") {
        options.body = opts;
      } else {
        options.query = opts;
      }
    },
    // onResponse相当于响应拦截
    onResponse({ response }) {
      console.log(response);
      const code = response._data.code;
      // 处理响应数据
      // if (code != 0) {
      //   isErr = true;
      //   // console.warn('=== error url: ', url, '\n params:', opts, '\n response:', response._data);
      //   throw new Error(response._data.message);

      //   // 处理服务端渲染时接口报错，可接入日志系统或者邮件系统，此处测试demo采取写入文件系统
      //   // fs.writeFileSync('error.txt', `error url:${url}\n response:${response._data.message}\n`, { flag: 'a' });
      // } else

      if (code === 100) {
        console.log("token error");
        isTokenError = true;
        // return { code: 100, message: "token过期" };
      } else {
        // isErr = false;
        // 这里可以根据后端返回的数据结构对返回数据进行处理，比如后端数据返回得比较深还有一层data结构，我这里就统一取出来，在后续接口请求的时候就直接能拿到了
        // response._data = response._data.data;
        response._data = response._data.data;
      }
    },
    onRequestError({ request, options, error }) {
      // 处理请求错误
      console.warn("request error", error);
    },
    onResponseError({ request, response, options }) {
      // 处理响应错误
      console.warn("request error", response);
    },
    server: false,
  });
  console.log("res.data", res.data);

  if (isTokenError) {
    console.log("token.....");
    alert("登录信息过期,请重新登录");
    // 清除登录信息
    const userInfo = useUseInfo();
    userInfo.value.tokenString = "";
    router.push("/login");
    return Promise.reject({ code: 100, message: "token过期" });
  }
  return res.data._value;
}
